import { NextRequest, NextResponse } from 'next/server'
import { projectQueries, userProjectLinkQueries } from '@/lib/database-prisma'
import { getUserFromRequest } from '@/lib/auth-utils'
import type { Project } from '@/types/database' // 确保Project类型已导入

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ projectId: string }> } // 参数名改为 projectId
) {
  try {
    const userInfo = await getUserFromRequest(request)
    if (!userInfo) {
      return NextResponse.json({ error: '未授权' }, { status: 401 })
    }

    const { projectId } = await params
    const projectIdNum = parseInt(projectId) // 直接使用projectIdNum
    
    const project = await projectQueries.findById.get(projectIdNum)
    
    if (!project) {
      return NextResponse.json({ error: '项目不存在' }, { status: 404 })
    }

    const hasAccess = await userProjectLinkQueries.checkUserInProject.get(userInfo.id, project.id)
    if (!hasAccess || hasAccess.count === 0) {
      return NextResponse.json({ error: '您没有访问此项目的权限' }, { status: 403 })
    }
    
    const members = await userProjectLinkQueries.getUsersByProjectId.all(project.id)
    
    return NextResponse.json(members)
  } catch (error) {
    console.error('获取项目成员失败:', error)
    return NextResponse.json({ error: '获取项目成员失败' }, { status: 500 })
  }
} 